﻿using AutoMapper;
using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Domain.zmc;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.Incominginformations;
using MyMES.Read.API.Dtos;
using System;

namespace MyMES.Read.API.Application.CommendHandlers.Incominginformations
{
    public class GetArrivalnoticeCommendHandlers : IRequestHandler<GetArrivalnoticeCommends, APIPaging<List<ArrivalnoSupplierDtos>>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<Arrivalnotice> arrivalnotRepository;
        private readonly IBaseRepository<Supplier> supplierRepository;
        private readonly IBaseRepository<StoragePurchase> stoRepository;

        public GetArrivalnoticeCommendHandlers(IMapper mapper, IBaseRepository<Arrivalnotice> arrivalnotRepository, IBaseRepository<Supplier> supplierRepository, IBaseRepository<StoragePurchase> stoRepository)
        {
            this.mapper = mapper;
            this.arrivalnotRepository = arrivalnotRepository;
            this.supplierRepository = supplierRepository;
            this.stoRepository = stoRepository;
        }



        /// <summary>
        /// 获取到货通知单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIPaging<List<ArrivalnoSupplierDtos>>> Handle(GetArrivalnoticeCommends request, CancellationToken cancellationToken)
        {
            try
            {
                APIPaging<List<ArrivalnoSupplierDtos>> result = new APIPaging<List<ArrivalnoSupplierDtos>>();
                result.Code = APIEnum.查询成功;
                result.Msg = "查询成功";
                var sql = " SELECT a.*,b.SupplierName,d.StoragePurchaseNo FROM arrivalnotice a LEFT JOIN supplier b on a.SupplierId=b.SupplierId  LEFT JOIN storagepurchase d on a.StoragePurchaseId=d.StoragePurchaseId  ORDER BY a.NoticeId;";


                var arriva = await arrivalnotRepository.SelectAsync(sql);
                var aupp = await supplierRepository.SelectAsync(sql);
                var stos = await stoRepository.SelectAsync(sql);
                var ress = mapper.Map<List<ArrivalnoSupplierDtos>>(arriva);
                var restype = mapper.Map<List<ArrivalnoSupplierDtos>>(aupp);
                var rstos = mapper.Map<List<ArrivalnoSupplierDtos>>(stos);

                if (!string.IsNullOrEmpty(request.NoticeNo))
                {
                    ress = ress.Where(x => x.NoticeNo.Contains(request.NoticeNo)).ToList();
                }
                if (!string.IsNullOrEmpty(request.NoticeName))
                {
                    ress = ress.Where(x => x.NoticeName.Contains(request.NoticeName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.StoragePurchaseNo))
                {
                    ress = ress.Where(x => x.StoragePurchaseNo.Contains(request.StoragePurchaseNo)).ToList();
                }
                if (!string.IsNullOrEmpty(request.SupplierName))
                {

                    ress = ress.Where(x => x.SupplierName.Contains(request.SupplierName)).ToList();
                }
                if (request.Deliverydate != null)
                {
                    var filterDate = request.Deliverydate.Value.Date.AddDays(1);
                    ress = ress.Where(x => x.Deliverydate.Date == filterDate).ToList();

                }
                if (request.NoticePurchase != null)
                {
                    ress = ress.Where(x => x.NoticePurchase == request.NoticePurchase).ToList();
                }

                foreach (var item in ress)
                {
                    item.SupplierName = restype.FirstOrDefault(x => x.SupplierId == item.SupplierId)?.SupplierName;
                    item.StoragePurchaseNo = rstos.FirstOrDefault(x => x.StoragePurchaseId == item.StoragePurchaseId)?.StoragePurchaseNo;
                }
                result.TotalCount = ress.Count();
                result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
                ress= ress.Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToList();
                result.data = ress;
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
